/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crawler;

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author marty
 */
public class Crawler {

    private static Crawler instance;

    public static Crawler getInstance() {
        if (instance == null) {
            instance = new Crawler();
        }
        return instance;
    }

    private Crawler() {
//
//
//        for (int multiverId = 1; multiverId < 100; multiverId++) {
//            try {
//                Document doc = Jsoup.connect("http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=" + multiverId).get();
//                String title = doc.title();
//                title = "";
//                Card karta = new Card(multiverId,
//                        doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_nameRow").select(".value").text(),
//                        doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_manaRow").select(".value").text(),
//                        Integer.parseInt(doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_cmcRow").select(".value").text()),
//                        doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_typeRow").select(".value").text(),
//                        doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_textRow").select(".value").select(".cardtextbox").text(),
//                        doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_flavorRow").select(".value").select(".cardtextbox").text(),
//                        doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_currentSetSymbol").select("a").text(),
//                        doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_rarityRow").select(".value").text(),
//                        doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_numberRow").select(".value").text(),
//                        doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_artistRow").select(".value").text(),
//                        title);
//                System.out.println(karta.toString());
//                System.out.println(karta.ENTER);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
    }

    public Card getCard(int multiverseId) {
        Card karta;
        try {
            Document doc = Jsoup.connect("http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=" + multiverseId).get();
            karta = new Card(multiverseId,
                    doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_nameRow").select(".value").text(),
                    doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_manaRow").select(".value").text(),
                    Integer.parseInt(doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_cmcRow").select(".value").text()),
                    doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_typeRow").select(".value").text(),
                    doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_textRow").select(".value").select(".cardtextbox").text(),
                    doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_flavorRow").select(".value").select(".cardtextbox").text(),
                    doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_currentSetSymbol").select("a").text(),
                    doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_rarityRow").select(".value").text(),
                    doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_numberRow").select(".value").text(),
                    doc.select("#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_artistRow").select(".value").text());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return karta;
    }
}
